﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using BLL;
using DAL.IRepository;
using Entities;
using DAL;
using System.Linq;

namespace WindowsApp.QLKeHoachGiangDay
{
    public partial class frmAddQuanLyKHGD : DevComponents.DotNetBar.Office2007Form
    {
        private IGeneralRepository<ChiTietKeHoachGiangDay> _repository;
        private ChiTietKeHoachGiangDayManager _ChiTietKHGDmanager;

        private QuanLyKeHoachGiangDayManager _manager;
        private QuanLyKeHoachGiangDay _kehoach;
        private bool _isUpdate;
        FrmQLKeHoachGiangDay cha;
        ChiTietKeHoachGiangDay _ChiTietKHGD;
        private bool _selectAll = false;
        public  void LoadChiTietKHGD()
        {
            _repository = new GeneralRepository<ChiTietKeHoachGiangDay>();
            _ChiTietKHGDmanager = new ChiTietKeHoachGiangDayManager(_repository);          
            dgvChiTietKeHoach.DataSource =ChiTietKeHoachGiangDay.Objects.Find(c => c.MaQL == _kehoach.MaQL).ToList();
        
        }
        public frmAddQuanLyKHGD()
        {
            InitializeComponent();
            dgvChiTietKeHoach.AutoGenerateColumns = false;
            
        }

        public frmAddQuanLyKHGD(QuanLyKeHoachGiangDayManager _man, FrmQLKeHoachGiangDay p, QuanLyKeHoachGiangDay _khgd = null)
            : this()
        {
            _manager = _man;
            _kehoach = _khgd;
            _isUpdate = (_khgd != null);
            cha = p;
        }
        public void frmAddQuanLyKHGD_Load(object sender, EventArgs e)
        {
             
            if (_isUpdate)
            {
                txtMaQL.Text = _kehoach.MaQL;
                txtNamHoc.Text = _kehoach.NamHoc;
                mtbHocKi.Text = _kehoach.HocKi;
                LoadChiTietKHGD();
                 
            }
            txtMaQL.ReadOnly = _isUpdate;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (!_isUpdate) _kehoach = new QuanLyKeHoachGiangDay();

            _kehoach.MaQL = txtMaQL.Text;
            _kehoach.NamHoc = txtNamHoc.Text;
            _kehoach.HocKi = mtbHocKi.Text;
            
             
            var vResult = _manager.Validate(_kehoach);
            if (vResult.Valid)
            {
                var aResult = _manager.InsertOrUpdate(_kehoach);
                if (aResult.Success)
                {
                    cha.btnLamMoi.PerformClick();
                    MessageBox.Show("Thêm thành công");
                    btnReset.PerformClick();
                }
                else
                    MessageBox.Show(aResult.GetErrorMessages());
            }
            else MessageBox.Show(vResult.ToString());
        }
       

        private void btnReset_Click(object sender, EventArgs e)
        {
            txtMaQL.Text = string.Empty;
            txtNamHoc.Text = string.Empty;
            mtbHocKi.Text ="1";
            dgvChiTietKeHoach.DataSource = null;
                      
            this.Text = "Thêm kế hoạch";
            txtMaQL.ReadOnly = false ;
        }
        private bool ThemQLKHTruoc()
        {
            _kehoach = new QuanLyKeHoachGiangDay();

            _kehoach.MaQL = txtMaQL.Text;
            _kehoach.NamHoc = txtNamHoc.Text;
            _kehoach.HocKi = mtbHocKi.Text;


            var vResult = _manager.Validate(_kehoach);
            if (vResult.Valid)
            {
                    var aResult = _manager.InsertOrUpdate(_kehoach);
                    if (aResult.Success)
                    {
                        DialogResult = DialogResult.OK;
                        return true;
                    }
                    else
                        MessageBox.Show(aResult.GetErrorMessages());
            }
            else MessageBox.Show(vResult.ToString());
            return false;
        }
        private void mnuThem_Click(object sender, EventArgs e)
        {
            if (ThemQLKHTruoc())
            {
                var frm = new frmAddChiTietKHGD(new ChiTietKeHoachGiangDayManager(ChiTietKeHoachGiangDay.Objects),this);
                frm.Text = "Thêm chi tiết kế hoạch giảng dạy";
                frm._maQL = txtMaQL.Text;
                //if (frm.ShowDialog() == DialogResult.OK)
                frm.ShowDialog();
                mnuRefresh.PerformClick();
            }        
        }

        private void mnuSua_Click(object sender, EventArgs e)
        {
            if (dgvChiTietKeHoach.SelectedRows.Count > 0)
            {
                
                  _ChiTietKHGD = new ChiTietKeHoachGiangDay();
                DataGridViewRow row = dgvChiTietKeHoach.SelectedRows[0];                 
                _ChiTietKHGD.MaKH = row.Cells[1].Value.ToString();
                _ChiTietKHGD.MaLop = row.Cells[2].Value.ToString();
                _ChiTietKHGD.GhepLop = row.Cells[3].Value.ToString();
                List<GiaoVien> ds= GiaoVien.Objects.Find(c=> c.HoTen== row.Cells[9].Value.ToString()).ToList();
                _ChiTietKHGD.MaGV = ds[0].MaGV;
                _ChiTietKHGD.MaMH = row.Cells[4].Value.ToString();

                _ChiTietKHGD.SoTietLTDaDay = Convert.ToInt32(row.Cells[10].Value.ToString());
                _ChiTietKHGD.SoTietLTConLai = Convert.ToInt32(row.Cells[11].Value.ToString());
                _ChiTietKHGD.SoTietTHDaDay = Convert.ToInt32(row.Cells[12].Value.ToString());
                _ChiTietKHGD.SoTietTHConLai = Convert.ToInt32(row.Cells[13].Value.ToString());
                _ChiTietKHGD.SoTuanConLai = Convert.ToInt32(row.Cells[14].Value.ToString());
                _ChiTietKHGD.MaQL = txtMaQL.Text ;

                var frm = new frmAddChiTietKHGD(new ChiTietKeHoachGiangDayManager( ChiTietKeHoachGiangDay.Objects),this,  _ChiTietKHGD);
                frm.Text = "Cập nhật chi tiết kế hoạch giảng dạy";
                frm._maQL = txtMaQL.Text;
                frm.ShowDialog();
                mnuRefresh.PerformClick();
            }
            else
                MessageBox.Show("Chưa chọn kế hoạch để sửa", "Thông báo");
        }

        public  void mnuRefresh_Click(object sender, EventArgs e)
        {
            dgvChiTietKeHoach.DataSource = dgvChiTietKeHoach.DataSource = ChiTietKeHoachGiangDay.Objects.Find(c => c.MaQL == txtMaQL.Text).ToList();
        }
        private void nnuXoa_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn có muốn xóa các chi tiết kế hoạch đã chọn?", "Xóa kế hoạch", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1) == DialogResult.Cancel)
                return;
            foreach (DataGridViewRow row in dgvChiTietKeHoach.Rows)
            {
                if (row.Cells[0].Value != null && row.Cells[0].Value.Equals(true))
                    ChiTietKeHoachGiangDay.Objects.Delete(ChiTietKeHoachGiangDay.Objects.GetById((row.Cells[1].Value.ToString())));
            }
            dgvChiTietKeHoach.DataSource = dgvChiTietKeHoach.DataSource = ChiTietKeHoachGiangDay.Objects.Find(c => c.MaQL == txtMaQL.Text).ToList();  
            mnuRefresh.PerformClick();

        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            if (!_isUpdate)
            {
                foreach (DataGridViewRow row in dgvChiTietKeHoach.Rows)
                {                   
                        ChiTietKeHoachGiangDay.Objects.Delete(ChiTietKeHoachGiangDay.Objects.GetById((row.Cells[1].Value.ToString())));
                }
                if (QuanLyKeHoachGiangDay.Objects.GetById(txtMaQL.Text) != null)
                    QuanLyKeHoachGiangDay.Objects.Delete(QuanLyKeHoachGiangDay.Objects.GetById(txtMaQL.Text));

            }
            this.Close();
        }
        private void dgvChiTietKeHoach_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.ColumnIndex == 0 && dgvChiTietKeHoach.Rows.Count > 0)
            {
                _selectAll = !_selectAll;
                for (int idx = 0; idx < dgvChiTietKeHoach.Rows.Count; idx++)
                {
                    dgvChiTietKeHoach.Rows[idx].Selected = _selectAll;
                    dgvChiTietKeHoach.Rows[idx].Cells[0].Value = _selectAll;
                }
            }
        }

        private void dgvChiTietKeHoach_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 0 && dgvChiTietKeHoach.Rows[e.RowIndex].Cells[0].Value != null)
            {
                bool selected = (bool)dgvChiTietKeHoach.Rows[e.RowIndex].Cells[0].Value;
                dgvChiTietKeHoach.Rows[e.RowIndex].Selected = selected;
            }
        }
        private void txtTim_TextChanged(object sender, EventArgs e)
        {

            var aResult=new ActionResultAsList<ChiTietKeHoachGiangDay>();
            if (rdbLopHoc.Checked)             
            {
              
                aResult = _ChiTietKHGDmanager.Find(s => s.MaLop.IndexOf(txtTim.Text) >= 0 && s.MaQL == txtMaQL.Text);//_manager.Find(s => s.NamHoc.IndexOf(txtNamHoc.Text) >= 0);
 
            }
            if (rdbSoTuan.Checked)
            {
              
                aResult = _ChiTietKHGDmanager.Find(s => s.SoTuanConLai >=Convert.ToInt32(txtTim.Text) && s.MaQL == txtMaQL.Text);//_manager.Find(s => s.NamHoc.IndexOf(txtNamHoc.Text) >= 0);
 
            }
            if (aResult.Success)
                dgvChiTietKeHoach.DataSource = aResult.Result;
        
               
           


        }

        private void rdbSoTuan_CheckedChanged(object sender, EventArgs e)
        {
            lblSoTuanConLai.Visible = true;
        }

        private void rdbLopHoc_CheckedChanged(object sender, EventArgs e)
        {
            lblSoTuanConLai.Visible = false;
        }

     
    }
}
